package server

import (
	"context"
	"longmen/server/config/global"
	messages2 "longmen/server/internal/rpc/tencentim/messages"
	"longmen/server/internal/rpc/tencentim/room_event"
	pb "longmen/server/pkg/protobuf"
)

//清除聊天
func (s *Server) ClearChatAdmin(c context.Context, req *pb.ClearChatReq) (*pb.ClearChatResp, error) {
	global.Log.Debugf("clearChat:%v", req)

	oNewReq := &messages2.ClearMessage{
		//因为协议的json不一样，在这里转一下再推
		ChatId: int(req.ChatId),
		RoomId: int(req.RoomId),
		UserId: int(req.UserId),
	}
	err := room_event.ClearChat(oNewReq)
	if err != nil {
		return &pb.ClearChatResp{Code: 1}, err
	}
	return &pb.ClearChatResp{Code: 0}, nil
}

//禁言
func (s *Server) BanUserAllChatAdmin(c context.Context, req *pb.BanUserChatReq) (*pb.ClearChatResp, error) {
	r := &messages2.BanUserChatReq{
		UserId:   int(req.UserId),
		BanType:  int(req.BanType),
		NickName: req.Nickname,
		TBan:     req.TBan,
		RoomId:   int(req.RoomId),
		Reason:   req.Reason,
	}
	var err error
	if req.TBan > 0 || req.RoomId == 0 { //禁言，或者全局解禁
		err = room_event.BanUserAllChanSync(r)
		if err != nil {
			return &pb.ClearChatResp{Code: 1}, err
		}
	} else if req.TBan == 0 && req.RoomId != 0 { //群组解禁
		err = room_event.BanUserGroup(r)
		if err != nil {
			return &pb.ClearChatResp{Code: 1}, err
		}
	}

	return &pb.ClearChatResp{Code: 0}, nil
}
